<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Settings Model
 * @description : Database functions related to innoTweets settings
 * 
 */
class Settings_model extends CI_Model {

  function __construct() {
	parent::__construct();
  }

  /**
   * Function to get counts for the main menu icons
   */
  function get_header_counts() {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	$date_dmyhis = date('Y-m-d H:i:s');
	$date_dmy = date('Y-m-d');
	$date_md = date('m-d');
	$query = "
        SELECT
        (SELECT COUNT(tweet_id) FROM tweets WHERE (tweet_status = 1) AND (DATE(tweet_date) = '{$date_dmy}') LIMIT 1) AS tweet_total_count,
        (SELECT COUNT(event_id) FROM events WHERE (event_status = 1) AND (DATE(event_date) = '{$date_dmy}') LIMIT 1) AS event_total_count,
        (SELECT COUNT(user_id) FROM users WHERE (user_status = 1) AND (user_resigned = 0) AND (user_is_user = 1) AND (DATE_FORMAT(DATE(user_birthday_date),'%m-%d') = '{$date_md}') LIMIT 1) AS birthday_total_count,
        (SELECT COUNT(user_id) FROM users WHERE (user_status = 1) AND (user_resigned = 0) AND (user_is_user = 1) AND (DATE_FORMAT(DATE(user_join_date),'%m-%d') = '{$date_md}') LIMIT 1) AS joinday_total_count,
        (SELECT COUNT(album_id) FROM albums WHERE (album_status = 1) AND (DATE(album_date) = '{$date_dmy}') LIMIT 1) AS album_total_count,
        (SELECT COUNT(poll_id) FROM polls WHERE (poll_status = 1) AND (DATE(poll_date) = '{$date_dmy}') LIMIT 1) AS poll_total_count,
        (SELECT COUNT(user_id) FROM users WHERE (user_status = 1) AND (user_online_mode = 1) AND ((UNIX_TIMESTAMP('{$date_dmyhis}') - UNIX_TIMESTAMP(user_online_date)) < 60 AND (UNIX_TIMESTAMP('{$date_dmyhis}') - UNIX_TIMESTAMP(user_online_date)) >= 0) LIMIT 1) AS online_total_count
        FROM tweets LIMIT 1
        ";
	$query_exec = $this->db->query($query);
	if ($query_exec == TRUE) {
	  $result_data = array();
	  $result_data['status'] = 1;
	  $result_data['result'] = $query_exec->row_array();
	  $result_data['count'] = $query_exec->num_rows();
	}
	return $result_data;
  }

  /**
   * Function to get counts for the main menu icons
   */
  function get_last_activity_date($date_dmy = null) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	$date_dmy_cln = (!empty($date_dmy)) ? $date_dmy : date('Y-m-d');
	$query = "
        SELECT
        (SELECT tweet_date FROM tweets WHERE (tweet_status = 1) AND (DATE(tweet_date) <= '{$date_dmy_cln}') ORDER BY tweet_date DESC LIMIT 1) AS tweet_date,
		(SELECT tweet_reply_date FROM tweets_reply WHERE (tweet_reply_status = 1) AND (DATE(tweet_reply_date) <= '{$date_dmy_cln}') ORDER BY tweet_reply_date DESC LIMIT 1) AS tweet_reply_date,
        (SELECT event_date FROM events WHERE (event_status = 1) AND (DATE(event_date) <= '{$date_dmy_cln}') ORDER BY event_date DESC LIMIT 1) AS event_date,
        (SELECT album_date FROM albums WHERE (album_status = 1) AND (DATE(album_date) <= '{$date_dmy_cln}') ORDER BY album_date DESC LIMIT 1) AS album_date,
        (SELECT poll_date FROM polls WHERE (poll_status = 1) AND (DATE(poll_date) <= '{$date_dmy_cln}') ORDER BY poll_date DESC LIMIT 1) AS poll_date,
		(SELECT poll_reply_date FROM polls_reply WHERE (DATE(poll_reply_date) <= '{$date_dmy_cln}') ORDER BY poll_reply_date DESC LIMIT 1) AS poll_reply_date,
		(SELECT wall_post_date FROM wall_posts WHERE (wall_post_status = 1) AND (DATE(wall_post_date) <= '{$date_dmy_cln}') ORDER BY wall_post_date DESC LIMIT 1) AS wall_post_date,
		(SELECT like_date FROM likes WHERE (DATE(like_date) <= '{$date_dmy_cln}') ORDER BY like_date DESC LIMIT 1) AS like_date,
		(SELECT comment_date FROM comments WHERE (comment_status = 1) AND (DATE(comment_date) <= '{$date_dmy_cln}') ORDER BY comment_date DESC LIMIT 1) AS comment_date
        FROM tweets LIMIT 1
        ";
	$query_exec = $this->db->query($query);
	if ($query_exec == TRUE) {
	  $result_data = array();
	  $result_data['status'] = 1;
	  $result_data['result'] = $query_exec->row_array();
	  $result_data['count'] = $query_exec->num_rows();
	}
	return $result_data;
  }

  function add_settings($config_id = NULL, $config_value = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($config_id)) {
	  $query_insr = array(
		  'settings_config_id' => $config_id,
		  'settings_config_value' => $config_value
	  );
	  $query_sql = $this->db->insert_string('settings', $query_insr);
	  $query_sql = str_replace('INSERT INTO', 'INSERT IGNORE INTO', $query_sql);
	  $query_exec = $this->db->query($query_sql);
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result'] = 1;
		$result_data['count'] = 1;
	  }
	}
	return $result_data;
  }

  function update_settings($config_id = NULL, $config_value = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($config_id)) {
	  $query_upda = array('settings_config_value' => $config_value);
	  $query_cond = array('settings_config_id' => $config_id);
	  $query_exec = $this->db->update('settings', $query_upda, $query_cond);
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result'] = 1;
		$result_data['count'] = 1;
	  }
	}
	return $result_data;
  }

  function del_settings($config_id = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($config_id)) {
	  $query_cond = array(
		  'settings_config_id' => $config_id
	  );
	  $query_exec = $this->db->delete('settings', $query_cond);
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result'] = 1;
		$result_data['count'] = 1;
	  }
	}
	return $result_data;
  }

  function get_settings($config_id = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($config_id)) {
	  $query_cond = array(
		  'settings_config_id' => $config_id
	  );
	  $query_exec = $this->db->get_where('settings', $query_cond);
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result'] = $query_exec->row_array();
		$result_data['count'] = $query_exec->num_rows();
	  }
	}
	return $result_data;
  }

}

?>
